package stark.app.function.watermark;

import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;
import com.stark.video.player.base.BaseJzVideoPlayerFragment;

import cn.jzvd.JzvdStd;
import stark.app.R;
import stark.app.databinding.FragmentWatermarkVideoPlayBinding;
import stark.common.basic.utils.StkPermissionHelper;

public class WatermarkVideoPlayFragment extends BaseJzVideoPlayerFragment<FragmentWatermarkVideoPlayBinding> {

    @NonNull
    @Override
    protected JzvdStd getJzvdStd() {
        return mDataBinding.jzvdStd;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_watermark_video_play;
    }

    @Override
    protected void initView() {
        super.initView();
        mDataBinding.btVideoSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btVideoSave) {
            StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                    .reqPermissionDesc("下载视频到本地相册，需申请文件存储权限，是否申请权限？")
                    .callback(new StkPermissionHelper.ACallback() {
                        @Override
                        public void onGranted() {
                            showDialog("下载中0%");
                            Downloader.newTask(mContext)
                                    .url(mVideoPath)
                                    .saveToPublic(true)
                                    //Android10及以上，保存到外部公共存储空间需提供文件名称
                                    .fileName(System.currentTimeMillis() + ".mp4")
                                    .start(mCallback);
                        }
                    })
                    .request();
        }
    }

    private Downloader.ICallback mCallback = new Downloader.ICallback() {
        @Override
        public void onStart(int downloadTaskId) {
        }

        @Override
        public void onProgress(long currentOffset, long totalLength, int percent) {
            showDialog("下载中" + percent + "%");
        }

        @Override
        public void onComplete(@NonNull Uri uri) {
            ToastUtils.showShort("下载成功，已保存到手机相册");
            dismissDialog();
        }

        @Override
        public void onFail(FailCause cause) {
            ToastUtils.showShort("下载失败");
            dismissDialog();
        }
    };
}
